//index.js
//获取应用实例
const _app = getApp()
let that;
let _crrentSelect;
let _content = ""

Page({
  data: {
    theam: "",
    emotion: "",
    city: "",
    street: "",
    address: "",
    picList: [],
    showTip: false,
    len: 0,
    //用户gps地址
    addr: "",
    //用户输入的内容
    content: "",
    //当前选中背景id
    selectImg: 0,
    //数据列表
    list: [],
    hasData: false
  },

  onLoad: function(options) {
    that = this;
    getLocation();
    loadBG();
  },

  onShow: () => {
    _content = "";
    if (_app.g.selectColor && _app.g.selectColor != "") {
      loadBG(_app.g.selectColor);
      _app.g.selectColor = "";
    }
    console.log("show");
  },

  publish: function() {
    if (_content == "") {
      wx.showToast({
        title: '心情不能为空',
        mask: true,
        duration:2000
      })
      return;
    }
    _app.request({
      url: _app.host + "note/add",
      method: "post",
      data: {
        // colorid: _crrentSelect.color_id,
        // themeid: _crrentSelect.id,
        colorid: 1,
        themeid: 1,
        text: _content
      },
      success: function(res) {
        _app.toast("发布成功");
        if (res.data.data && res.data.data != "") {
          wx.navigateTo({
            url: '/pages/share/share?id=' + res.data.data,
          })
        }
      },
      fail: function(res) {
        _app.alert(res.data.msg);
      }
    });
  },

  gotoColor: function() {
    wx.navigateTo({
      url: '../colorChoose/colorChoose',
    })
  },

  getLen: function(e) {
    _content = e.detail.value;

    if (_content.length > 20) {
      this.setData({
        showTip: true
      })
    }
    var len = parseInt(_content.length);
    this.setData({
      len: len
    })
  },

  changeImg: (evt) => {
    for (let i in that.data.picList) {
      if (that.data.picList[i]["id"] == evt.target.dataset.id) {
        _crrentSelect = that.data.picList[i];
        that.setData({
          theam: _crrentSelect["pic_url"]
        });
        break;
      }
    }
  }
})

function loadBG(value) {
  wx.showNavigationBarLoading();
  let url = _app.host + "theme/gettheme";
  if (value && value != "") {
    url += "?color=" + value;
  }
  _app.request({
    url: url,
    method: "get",
    data: {},
    success: getBGSuccess,
    fail: getBGFail
  });
}

function getBGSuccess(res) {
  wx.hideNavigationBarLoading();
  that.setData({
    hasData: true
  });
  if (res.data.code == "100000") {
    let temArr = [];
    for (let i in res.data.data) {
      if (i < 6) {
        temArr.push(res.data.data[i]);
      }
    }
    _crrentSelect = temArr[0];
    that.setData({
      picList: temArr,
      theam: temArr[0]["pic_url"]
    })
  } else {
    wx.showModal({
      title: '温馨提示',
      content: '获取数据错误，请重试',
    })
  }
}

function getBGFail(res) {
  wx.hideNavigationBarLoading();
  _app.log(res, "loadBG Fail");
}

function getLocation() {
  wx.getLocation({
    success: function(res) {
      let map = new _app.map({
        key: "CBRBZ-DLB3G-GDMQB-IMHQK-F7CGJ-7ZB6F"
      });
      map.reverseGeocoder({
        location: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success: (res) => {
          if (res.message == "query ok") {
            that.setData({
              address: res.result.address
            });
          }
        }
      });
    },
    fail: function(res) {

    }
  })
}